package Lambada;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-11-04
 * Time:9:52
 */

//lambada表达式 是一个匿名函数
    /*
    正常的函数 返回值  函数名 （参数）

    函数式接口

    了解了变量捕获才能掌握它的作用域
     */


    //函数式接口
@FunctionalInterface
    interface NoParameterNoReturn{
        void test();
}
interface OneParameterNoReturn{
    void test(int a);
}
interface MoreParameterNoReturn{
    void test(int a,int b);
}
interface NoParameterReturn{
    int test();
}
interface OneParameterReturn{
    int test(int a);
}
interface MoreParameterReturn{
    int test(int a,int b);
}
class Test{
    public void func(){
        System.out.println("func()");
    }
}

//lambada在集合中的使用
public class TestDemo {
    public static  void func(int a){

    }
    public static  void func(int a,int b){

    }
    public static  void func(int... pars){
        for (int i = 0; i < pars.length; i++) {

        }
    }
    public static  void func2(int[] array){
        array[0] = 99;
    }
    public static  void func3(int[] array){
        array = new int[10];
    }
    public static void main(String[] args) {
        func(1,2,3,4,5,6,7,8,9);
    }

    public static void main6(String[] args) {
        HashMap<Integer,String> map = new HashMap<>();
        map.put(1,"hello");
        map.put(2,"bit");
        map.put(3,"hello");
        map.put(4,"lambada");

        map.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer integer, String s) {
                System.out.println("key : " + integer + "val : " + s);
            }
        });
        map.forEach((a,b) -> System.out.println("key : " + a + "val : " + b) );
    }
    public static void main5(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("bit");
        list.add("hello");
        list.add("lambada");
//        list.forEach(new Consumer<String>() {
//            @Override
//            public void accept(String s) {
//                System.out.println(s);
//            }
//        });
//        list.forEach(s -> System.out.println(s));
//    list.sort(new Comparator<String>() {
//        @Override
//        public int compare(String o1, String o2) {
//            return o1.compareTo(o2);
//        }
//    });
        list.forEach(s -> System.out.println(s));
        list.sort((o1, o2) ->{return o1.compareTo(o2);} );
        System.out.println("===================");
        list.forEach(s -> System.out.println(s));
    }

    public static void main4(String[] args) {
        int a = 10;

        //lambada的变量捕获
        NoParameterNoReturn noParameterNoReturn = () ->{
//            a = 99;
            System.out.println("haha");
            System.out.println(a);
        };
        noParameterNoReturn.test();
    }
    public static void main3(String[] args) {
        final int a = 10;
        new Test(){
            @Override
            public void func(){
//                 a = 99;//一定是没有发生改变的量
                System.out.println("这是重写的内部类方法!");
                System.out.println("我是个常量或则没有被改变的量" + a);
                //a就是被捕获的变量 一定是没有发生改变的变量
            }
        }.func();
    }
    public static void main2(String[] args) {
//        NoParameterNoReturn noParameterNoReturn2 = () -> System.out.println("haha");
//        noParameterNoReturn2.test();
//
//        OneParameterNoReturn oneParameterNoReturn = (int a) -> System.out.println(a);
//        oneParameterNoReturn.test(10);
//        MoreParameterNoReturn moreParameterNoReturn = (int a,int b) -> System.out.println(a+b);
//        moreParameterNoReturn.test(10,20);

        //你要把一个函数改成更lambada你必须要知道这个方法是怎么实现的

//        NoParameterReturn noParameterReturn = () -> {return 10;};
//        NoParameterReturn noParameterReturn = () -> 10;
//        System.out.println(noParameterReturn.test());

//        OneParameterReturn oneParameterReturn = (int a) -> a;
//        OneParameterReturn oneParameterReturn = (a) -> a;
//        int val = oneParameterReturn.test(19);
//        System.out.println(val);
        MoreParameterReturn moreParameterReturn = (int a,int b) -> a+b;
        System.out.println(moreParameterReturn.test(10, 20));
//        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
//               return o1 - o2;
//            }
//        });
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((o1, o2) -> {return o1 -o2;});

    }
    public static void main1(String[] args) {
        NoParameterNoReturn noParameterNoReturn1 = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("haha");
                System.out.println("haha2");
            }

        };
        noParameterNoReturn1.test();
        System.out.println("=====================");
        NoParameterNoReturn noParameterNoReturn2 = () -> {
            System.out.println("haha");
            System.out.println("haha2");
        };
        noParameterNoReturn2.test();
    }
}
